{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc67bb2d",
   "metadata": {},
   "source": [
    "## 创建人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "69a273de",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_key = \"N4jJQNtBkzNgVcOqZV-P34mKhYPm0hk2\"\n",
    "API_secret = \"nNhPtXGnphFeKs0o7E_tALIoyuQ2hh7e\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "efa5357a",
   "metadata": {},
   "outputs": [],
   "source": [
    "FaceSet_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret\n",
    "}\n",
    "r = requests.post(FaceSet_url,params=payload)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "676e975a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "27dd1796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '48d8b344ada612852eb776d742288db8',\n",
       " 'time_used': 176,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648971288,8bab6a3b-669e-420d-8b3a-c3a3a5c8fd56',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "72b4fb9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'48d8b344ada612852eb776d742288db8'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token = r.json()[\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcee8552",
   "metadata": {},
   "source": [
    "## 更改人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4472c86c",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"faceset_token\":faceset_token,\n",
    "    \"new_outer_id\":\"12426\",\n",
    "    \"display_name\":\"12栋426\",\n",
    "    \"user_data\":\"4人\",\n",
    " }\n",
    "r = requests.post(update_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "804cf49b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "0c8a95ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '7a8cfa4b2d8cd65b1ba0b2b42d5ada7d',\n",
       " 'request_id': '1648480536,17e267f6-f306-43e2-aeb1-37e794442ec8',\n",
       " 'time_used': 81,\n",
       " 'outer_id': '12426'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b0cc4b2",
   "metadata": {},
   "source": [
    "## 获取人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8da8be8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"faceset_token\":faceset_token    \n",
    "}\n",
    "r = requests.post(update_url,params=payload)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "b57fa847",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e300131c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '48d8b344ada612852eb776d742288db8',\n",
       " 'tags': '',\n",
       " 'time_used': 111,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648971292,41daee56-a43f-44b8-8129-97f339c8ae0a',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daf75aae",
   "metadata": {},
   "source": [
    "## 获取舍友face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ede115af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8f7927fb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648971297,c5ea19f8-ffca-49b8-a7b5-a987c9b3785d',\n",
       " 'time_used': 276,\n",
       " 'faces': [{'face_token': '58fea894ef883dc9a38667281892a295',\n",
       "   'face_rectangle': {'top': 522, 'left': 217, 'width': 488, 'height': 488}}],\n",
       " 'image_id': 't0fU+lQW7x45oFU7KufRCg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_wcc = detect.face_detect(API_key,API_secret,\"wcc.jpg\")\n",
    "face_wcc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "971f03e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'58fea894ef883dc9a38667281892a295'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_wcc = face_wcc[\"faces\"][0][\"face_token\"]\n",
    "face_token_wcc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7f7b4144",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648971297,cc8d851f-4aa5-40bb-99e7-184261021337',\n",
       " 'time_used': 134,\n",
       " 'faces': [{'face_token': '998ce37634054aeacbd1980dba65a27c',\n",
       "   'face_rectangle': {'top': 541, 'left': 127, 'width': 506, 'height': 506}}],\n",
       " 'image_id': 'W0L5lbWJIbCA4gyQ8Xv2Yw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_cly = detect.face_detect(API_key,API_secret,\"cly.jpg\")\n",
    "face_cly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "571de3cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'998ce37634054aeacbd1980dba65a27c'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_cly = face_cly[\"faces\"][0][\"face_token\"]\n",
    "face_token_cly"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2bddfb4b",
   "metadata": {},
   "source": [
    "## 上传舍友信息到人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f467ab84",
   "metadata": {},
   "outputs": [],
   "source": [
    "update_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"faceset_token\":faceset_token,\n",
    "    \"face_tokens\":face_token_wcc,\n",
    "    \"face_tokens\":face_token_cly\n",
    "    \n",
    " }\n",
    "r = requests.post(update_url,params=payload)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7e48ec96",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '48d8b344ada612852eb776d742288db8',\n",
       " 'time_used': 559,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648971360,5d40efa3-76a8-4892-959e-07702b28bd8e',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21d1f9b6",
   "metadata": {},
   "source": [
    "## 更新人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "85d33881",
   "metadata": {},
   "outputs": [],
   "source": [
    "baseurl = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    \"api_key\":API_key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"faceset_token\":faceset_token,\n",
    "}\n",
    "r = requests.post(baseurl,params=payload)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1a2ca3d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "7d167ddd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '48d8b344ada612852eb776d742288db8',\n",
       " 'tags': '',\n",
       " 'time_used': 121,\n",
       " 'user_data': '',\n",
       " 'display_name': '',\n",
       " 'face_tokens': ['998ce37634054aeacbd1980dba65a27c'],\n",
       " 'face_count': 1,\n",
       " 'request_id': '1648971362,422e705f-b4f6-4b3e-bc84-ee8352c89543',\n",
       " 'outer_id': ''}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f41acd4",
   "metadata": {},
   "source": [
    "## 宿舍人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "95af2ad8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import room_search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "6a34ce86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module room_search:\n",
      "\n",
      "NAME\n",
      "    room_search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_key, API_sercret, faceset_token, image_path)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\lenovo\\jupyter project\\room_search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(room_search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "a4f08a27",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'image_id': 't0fU+lQW7x45oFU7KufRCg==',\n",
       " 'faces': [{'face_rectangle': {'width': 488,\n",
       "    'top': 522,\n",
       "    'left': 217,\n",
       "    'height': 488},\n",
       "   'face_token': '59ac8f045026c290e04b8b69923bb3f7'}],\n",
       " 'time_used': 636,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'request_id': '1648971429,b900ce80-ae4b-4da0-9762-03ee070e1cbe',\n",
       " 'results': [{'confidence': 39.164,\n",
       "   'user_id': '',\n",
       "   'face_token': '998ce37634054aeacbd1980dba65a27c'}]}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wcc_search = room_search.face_search(API_key,API_secret,faceset_token,\"wcc.jpg\")\n",
    "wcc_search"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26f3b0b2",
   "metadata": {},
   "source": [
    "## 宿舍人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "e143a607",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "c6856cf7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'998ce37634054aeacbd1980dba65a27c'"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "wcc_search_token = wcc_search[\"results\"][0]['face_token']\n",
    "wcc_search_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "cb48ecdf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648972053,83505a7f-f09c-4256-8037-bfb6849f5d1c',\n",
       " 'time_used': 623,\n",
       " 'confidence': 37.371,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = compare.face_compare(API_key,API_secret,wcc_search_token,face_token_wcc)\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "6805fc55",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "37.371"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confidence = result['confidence']\n",
    "confidence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d4e6727f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no\n"
     ]
    }
   ],
   "source": [
    "if confidence > 50 :\n",
    "    print(\"yes\")\n",
    "else :\n",
    "    print (\"no\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aea5de01",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
